<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of BLanguage
 *
 * @author I062261
 */
class BTLanguage extends BWEntity {

    //put your code here
    public $name;

    const ENGLIST = 'en_US';
    const CHINESE = 'zh_CN';

    public function __construct($item = NULL, $context = NULL) {
        if (is_string($item)) {
            $this->name = $item;
        }
        parent::__construct($item, $context);
    }

    /**
     * get user's language setting
     * @param type $data
     */
    public function getNativeLanguage($data = NULL) {
        $app = Blyn::app()->getActiveApp();
        $user = Blyn::app()->getCurrentUser();
        $userAttribute = 'user_attribute_' . $user->getId();
        $userNativeLanguageAttribute = $userAttribute . '.' . 'nativeLanguage';

        $userNativeLanguage = $app->getAttribute($userNativeLanguageAttribute);
        if ($userNativeLanguage instanceof BAttribute)
            $userNativeLanguage = $userNativeLanguage->getValue();
        if ($userNativeLanguage == NULL) {
            $app->addAttribute($userNativeLanguageAttribute, self::CHINESE);
            $userNativeLanguage = self::CHINESE;
        }

        return $userNativeLanguage;
    }

    public function getForeignLanguage($data = NULL) {
        $app = Blyn::app()->getActiveApp();
        $user = Blyn::app()->getCurrentUser();
        $userAttribute = 'user_attribute_' . $user->getId();

        $userForeignLanguageAttribute = $userAttribute . '.' . 'foreignLanguage';

        $userForeignLanguage = $app->getAttribute($userForeignLanguageAttribute)->getValue();
        if ($userForeignLanguage == NULL) {
            $app->addAttribute($userForeignLanguageAttribute, self::ENGLIST);
            $userForeignLanguage = self::ENGLIST;
        }

        return $userForeignLanguage;
    }

    public function addNativeLanguage($language) {
        $app = Blyn::app()->getActiveApp();
        $user = Blyn::app()->getCurrentUser();
        $userAttribute = 'user_attribute_' . $user->getId();
        $userNativeLanguageAttribute = $userAttribute . '.' . 'nativeLanguage';

        $app->addAttribute($userNativeLanguageAttribute, $language);
    }

    public function addForeignLanguage($language) {
        $app = Blyn::app()->getActiveApp();
        $user = Blyn::app()->getCurrentUser();
        $userAttribute = 'user_attribute_' . $user->getId();
        $userForeignLanguageAttribute = $userAttribute . '.' . 'foreignLanguage';

        $app->addAttribute($userForeignLanguageAttribute, $language);
    }

    public function getCategory($name = NULL) {

        $category = new BWCategory($this);

        $category->name = 'language_category_root';
        $category->load();

        if ($category->getId() == NULL) {
            $category = BWCategory::getRoot()->addChild($category);
            //$category->save();
        }

        if (is_string($name) && $name != NULL) {
            $category->getChild($name);
        }

        return $category;
    }

    public function getRepository($data = NULL) {
        $repository = parent::getRepository($data);
        $repository->setModel('BTMLanguage');
        return $repository;
    }

    public function getModelClass($data = NULL) {
        return 'BTMLanguage';
    }

}

?>
